package com.zyq.chessdemo.demo2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

public class GamePanel extends JPanel {
    public static void main(String[] args) {
    }

    public GamePanel() {
        createChesses();
    }

    private Chess[] chesses = new Chess[32];

    // 选中的棋子
    private Chess selectedChess;


    /**
     * 根据网格坐标p对象查找棋子对象
     *
     * @param p
     * @return
     */
    public Chess getChessByP(Point p) {
        System.out.println("Point p ---" + p);
        for (Chess item : chesses) {
            if (item.getP().equals(p)) {
                return item;
            }
        }
        return null;
    }

    // 单一职责
    @Override
    public void paint(Graphics g) {
        // super.paint(g);
        System.out.println("为什么又写Jpanel,为什么执行2次，paint");

        // 背景
        String bgPath = "pic" + File.separator + "qipan.jpg";
        Image bgimage = Toolkit.getDefaultToolkit().getImage(bgPath);
        g.drawImage(bgimage, 0, 0, this);

        drawChesses(g);

        // for (int i = 0; i < chesses.length; i++) {
        //     System.out.println("chesses ---" + i + " " + chesses[i]);
        // }

        /**
         * 操作棋子：
         * 1. 点击棋盘
         *  :事件
         * 2. 判断点击的地方是否有棋子
         *  :点击的点的坐标与数组存储的点遍历对比
         * 3. 区分第一次选择、重新选择、移动、吃子；
         */

        if (selectedChess != null) {
            selectedChess.drawRect(g);
        }

        // 添加点击事件
        addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                // System.out.println("点击的位置：" + e.getX() + "," + e.getY());

                // Chess c = new Chess();
                // Point point = c.getPointFromXY(e.getX(), e.getY());
                // System.out.println("点击的棋子：" + point);

                // 改为静态方法
                Point p = Chess.getPointFromXY(e.getX(), e.getY());
                // System.out.println("点击的棋子网格坐标：" + p);

                // Chess c = getChessByP(p);
                // System.out.println("点击的棋子对象网格位置c---" + c);


                // 手中没有棋子
                if (selectedChess == null) {
                    if (getChessByP(p) != null) {
                        selectedChess = getChessByP(p);
                    }
                } else {
                    // 重新选择
                    // 移动、吃子
                    Chess c = getChessByP(p);
                    if (c != null) {
                        // 重新选择还是吃子
                        // 重新选择是自己
                        // 吃子必须是对面
                        if (selectedChess.getPlayer() == c.getPlayer()) {
                            // 重新选择
                            selectedChess = c;
                            System.out.println("重新选择");
                        } else {
                            // 吃子
                            System.out.println("吃子");

                        }

                    } else {
                        // 移动
                        // selectedChess = getChessByP(p);
                        System.out.println("移动");
                        if (selectedChess.isAbleMove(p, GamePanel.this)) {
                            selectedChess.setP(p);
                            selectedChess = null;
                        }
                    }
                }

                System.out.println("selectedChess---" + selectedChess);
                // 刷新棋盘
                repaint();

            }
        });

        // String che0Path = "pic" + File.separator + "che0.png";
        // Image che0image = Toolkit.getDefaultToolkit().getImage(che0Path);
        // // g.drawImage(che0image, 0, 0, this);
        // g.drawImage(che0image, 5, 5,30,30, this);
        //
        // String ma0Path = "pic" + File.separator + "ma0.png";
        // Image ma0image = Toolkit.getDefaultToolkit().getImage(ma0Path);
        // g.drawImage(ma0image, 5+40, 5,30,30, this);

        // String[] names = {"che0", "ma0", "xiang0", "shi0", "boss0", "shi0", "xiang0", "ma0", "che0",
        //         "pao0", "pao0", "bing0", "bing0", "bing0", "bing0", "bing0"};

        // 方式1，网格大小
        // int[] xs = {5, 45, 85, 125, 165, 205, 245, 285, 325,
        //         45, 285, 5, 85, 165, 245, 325};
        // int[] ys = {5, 5, 5, 5, 5, 5, 5, 5, 5,
        //         85, 85,
        //         125, 125, 125, 125, 125};
        // for (int i = 0; i < names.length; i++) {
        //     String path = "pic" + File.separator + names[i] + ".png";
        //     Image image = Toolkit.getDefaultToolkit().getImage(path);
        //     // g.drawImage(image, 5 + (40 * i), 5, 30, 30, this);
        //     g.drawImage(image, xs[i], ys[i], 30, 30, this);
        // }

        // String[] names = {"che", "ma", "xiang", "shi", "boss", "shi", "xiang", "ma", "che",
        //         "pao", "pao", "bing", "bing", "bing", "bing", "bing"};

        // 棋子阵营
        // int player = 0;
        // String suffix = ".png";
        // 方式2
        // int[] xs = {1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 8, 1, 3, 5, 7, 9};
        // int[] ys = {1, 1, 1, 1, 1, 1, 1, 1, 1,
        //         3, 3,
        //         4, 4, 4, 4, 4};
        // // 棋子边框
        // int margin = 20;
        // // 棋子大小
        // int size = 30;
        // // 棋盘间隔
        // int space = 40;
        // for (int i = 0; i < names.length; i++) {
        //     String path = "pic" + File.separator + names[i] + player + suffix;
        //     Image image = Toolkit.getDefaultToolkit().getImage(path);
        //     // g.drawImage(image, 5 + (40 * i), 5, 30, 30, this);
        //     // g.drawImage(image, 5 + 40 * (xs[i] - 1), 5 + 40 * (ys[i] - 1), 30, 30, this);
        //     g.drawImage(image, margin - size / 2 + space * (xs[i] - 1),
        //             margin - size / 2 + space * (ys[i] - 1),
        //             size, size, this);
        // }
        //
        // player = 1;
        // for (int i = 0; i < names.length; i++) {
        //     String path = "pic" + File.separator + names[i] + player + suffix;
        //     Image image = Toolkit.getDefaultToolkit().getImage(path);
        //     // g.drawImage(image, 5 + (40 * i), 5, 30, 30, this);
        //     // g.drawImage(image, 5 + 40 * (xs[i] - 1), 5 + 40 * (ys[i] - 1), 30, 30, this);
        //     // g.drawImage(image, margin - size / 2 + space * (xs[i] - 1),
        //     g.drawImage(image, margin - size / 2 + space * (reserveX(xs[i]) - 1),
        //             margin - size / 2 + space * (reserveY(ys[i]) - 1),
        //             size, size, this);
        // }

        //     方式3


    }


    /**
     * 创建棋子
     */
    private void createChesses() {

        String[] names = {"che", "ma", "xiang", "shi", "boss", "shi", "xiang", "ma", "che",
                "pao", "pao", "bing", "bing", "bing", "bing", "bing"};

        Point[] ps = {new Point(1, 1), new Point(2, 1), new Point(3, 1), new Point(4, 1),
                new Point(5, 1), new Point(6, 1), new Point(7, 1), new Point(8, 1), new Point(9, 1),
                new Point(2, 3), new Point(8, 3),
                new Point(1, 4), new Point(3, 4), new Point(5, 4), new Point(7, 4), new Point(9, 4)
        };

        for (int i = 0; i < names.length; i++) {
            Chess c = new Chess();
            c.setName(names[i]);
            c.setP(ps[i]);
            c.setPlayer(0);
            // c.draw(g, this);
            // 保存棋子到数组
            chesses[i] = c;
            // System.out.println("c ---"+ i + c );
        }

        for (int i = 0; i < names.length; i++) {
            Chess c = new Chess();
            c.setName(names[i]);
            c.setP(ps[i]);
            c.setPlayer(1);
            c.reserve();
            chesses[i + 16] = c;
            // c.draw(g, this);
        }

    }

    /**
     * 绘制棋子
     *
     * @param g
     */
    private void drawChesses(Graphics g) {
        for (Chess item : chesses) {
            item.draw(g, this);
        }
    }


    public int reserveX(int x) {
        int temp;
        temp = 10 - x;
        return temp;
    }

    public int reserveY(int y) {
        int temp;
        temp = 11 - y;
        return temp;
    }


    // @Override
    // 父类有的方法，才能加@Override
    // 子类特有的方法，不能加@Override
    // public void myPaint(Graphics g) {
    //     // super.paint(g);
    // }


}


